

# from collections import reduce
from functools import reduce

class Solution:

    def productExceptSelf(self, nums: list):
        n = len(nums)
        ans = [0] * n
        cnt0 = nums.count(0)
        if cnt0 > 1:
            return ans
        

        if cnt0 == 1:

            pos0 = nums.index(0)
            if pos0 == 0:
                ans[pos0] = reduce(lambda x, y: x * y, nums[1 :])
            elif pos0 == n - 1:
                ans[pos0] = reduce(lambda x, y: x * y, nums[:n - 1])
            else:

                ans[pos0] = reduce(lambda x, y: x * y, nums[:pos0]) * reduce(lambda x, y: x * y, nums[pos0 + 1:])
            
            return ans

        first = reduce(lambda x, y: x * y, nums)
        # print(first)
        # ans[0] = first
        for i in range(0, n):
            ans[i] = first // nums[i]

        return ans




if __name__ == "__main__":

    nums = [1, 2, 3, 4]
    sol = Solution()
    ans = sol.productExceptSelf(nums)
    print(ans)

